package com.earthchen.mattress.dao;

import com.earthchen.mattress.domain.MattressData;

import java.sql.SQLException;
import java.util.Arrays;

/**
 * 床垫数据访问层
 *
 * @author: EarthChen
 * @date: 2018/04/24
 */
public class MattressDao {

    /**
     * 保存床垫数据
     *
     * @param mattressData
     * @return
     */
    public boolean saveMattressData(MattressData mattressData) {
        String sql = "insert into mattress (sn, fv, state, heartbeat, breath, wet, odor, weight, position, createTime) values (?,?,?,?,?,?,?,?,?,?)";
        DBHelper dbHelper = new DBHelper(sql);
        try {
            dbHelper.pst.setString(1, mattressData.getSn());
            dbHelper.pst.setInt(2, mattressData.getFv());
            dbHelper.pst.setString(3, mattressData.getState());
            dbHelper.pst.setInt(4, mattressData.getHeartbeat());
            dbHelper.pst.setInt(5, mattressData.getBreath());
            dbHelper.pst.setBoolean(6, mattressData.isWet());
            dbHelper.pst.setInt(7, mattressData.getOdor());
            dbHelper.pst.setInt(8, mattressData.getWeight());
            dbHelper.pst.setString(9, Arrays.toString(mattressData.getPosition()));
            dbHelper.pst.setTimestamp(10, new java.sql.Timestamp(mattressData.getCreateTime().getTime()));
            int success = dbHelper.pst.executeUpdate();
            return success > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbHelper.close();
        }
        return false;
    }

}
